package com.free.solon.common.model;

import lombok.Data;
import org.noear.solon.annotation.Note;

/**
 * 统一返回对象
 *
 * @Author: hao.ding@insentek.com
 * @Date: 2024/5/21 15:30
 */
@Data
public class R<T> {

    public static int SUCCEED_CODE = 200;

    public static int FAILURE_CODE = 500;

    private int code;

    private String message;

    private T data;

    public R() {
        this.code = SUCCEED_CODE;
        this.message = "";
    }

    public R(T data) {
        this.code = SUCCEED_CODE;
        this.message = "";
        this.data = data;
    }

    public R(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    @Note("成功的空结果")
    public static <T> R<T> succeed() {
        return new R<>(SUCCEED_CODE, "");
    }

    @Note("成功的结果")
    public static <T> R<T> succeed(T data) {
        return new R(data);
    }

    @Note("成功的结果")
    public static <T> R<T> succeed(T data, String message) {
        return new R(SUCCEED_CODE, message, data);
    }

    @Note("成功的结果")
    public static <T> R<T> succeed(T data, int code) {
        return new R(code, "", data);
    }

    @Note("失败的空结果")
    public static <T> R<T> failure() {
        return new R(FAILURE_CODE, "");
    }

    @Note("失败的结果")
    public static <T> R<T> failure(int code) {
        return failure(code, "");
    }

    @Note("失败的结果")
    public static <T> R<T> failure(int code, String message) {
        return new R(code, message);
    }

    @Note("失败的结果")
    public static <T> R<T> failure(int code, String message, T data) {
        return new R(code, message, data);
    }

    @Note("失败的结果")
    public static <T> R<T> failure(String message) {
        return new R(FAILURE_CODE, message);
    }
}
